﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using System.Security.Cryptography;
using System.Net.Mail;

namespace eVoteClient.util
{
    class Encryption
    {
        public static BigInteger paillierEncrypt(BigInteger b,BigInteger g, BigInteger n, int answerid)
        {
            

            BigInteger actualMessage = BigInteger.Pow(b, answerid);

            //Now encrypt actualMessage
            
            //getting a random biginteger
            int bits_of_n = (int)Math.Floor(BigInteger.Log(n, 2)) - 1;
            var rng = new RNGCryptoServiceProvider();
            byte[] bytes = new byte[bits_of_n / 8];
            rng.GetBytes(bytes);
            
            BigInteger r = new BigInteger(bytes);
            if (BigInteger.Compare(r, BigInteger.Zero) < 0)
                r = BigInteger.Negate(r);
            BigInteger n2 = BigInteger.Pow(n, 2);

            //this is (g^m mod n^2)
            BigInteger gmn2= BigInteger.ModPow(g, actualMessage,n2);

            //this is (r^n mod n^2)
            BigInteger rnn2 = BigInteger.ModPow(r, n, n2);

            //multiply them
            BigInteger result = BigInteger.Multiply(gmn2, rnn2);
            

            //from the previous result, apply mod n^2
            result = BigInteger.ModPow(result, 1, n2);

            //throw new Exception("ANSWER : "+ answerid+"\n B " + b.ToString() + " N " + n.ToString() + " r " + r.ToString()+ " G "+g.ToString()+"\n"
              //              +" n2 "+n2.ToString() + " gmn2 " + gmn2.ToString()+ " rnn2 "+rnn2.ToString() + " result "+result.ToString());
            return result;
            
        }

        public static string sh1(string input)
        {
            SHA1CryptoServiceProvider sh1sp = new SHA1CryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(input);
            byte[] hashedBytes = sh1sp.ComputeHash(bytes);
            //string output = Encoding.UTF8.GetString(hashedBytes);
            string output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLowerInvariant();

            return output;
        }

        public static bool sendMail(string to,string subject, string body)
        {
            var message= new MailMessage();
            message.To.Add(to);
            message.Subject = subject;
            message.From = new System.Net.Mail.MailAddress("gucevoting@gmail.com");
            
            message.Body = body;
            //"Your vote has been submitted successfully.\n This email is sent from the application you used to submit your vote.\n The code for your vote is "
            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            smtp.Credentials = new System.Net.NetworkCredential("gucevoting@gmail.com", "A*Logic<>Hn");
            smtp.EnableSsl = true;
            
                smtp.Send(message);
                return true;
            
        }
    }

}
